package jp.eisbahn.mgapi.api.message;

import java.text.ParseException;
import java.util.Date;

import org.json.simple.JSONObject;

import jp.eisbahn.mgapi.api.Model;
import jp.eisbahn.mgapi.api.User;
import jp.eisbahn.mgapi.util.DateUtils;

/**
 * This class represents a message.
 * @author Yoichiro Tanaka
 */
public class Message extends Model {

    /** The user object. */
    private User sender;

    /**
     * Initialize this instance.
     * @param newOriginal The JSON object.
     */
    protected Message(final JSONObject newOriginal) {
        super(newOriginal);
        sender = new User(getPropertyJSONObject("sender"));
    }

    /**
     * Retrieve the ID.
     * @return The ID string.
     */
    public final String getId() {
        return getPropertyString("id");
    }

    /**
     * Retrieve the status.
     * @return The status value.
     */
    public final Status getStatus() {
        String status = getPropertyString("status");
        if (status.equals("new")) {
            status = "unread";
        }
        return Status.valueOf(status);
    }

    /**
     * Retrieve the timeSent.
     * @return The timeSent value as Date object.
     * @throws ParseException If the date string could not be parsed.
     */
    public final Date getTimeSent() throws ParseException {
        String timeSent = getPropertyString("timeSent");
        if (timeSent != null) {
            return DateUtils.convertDate(timeSent);
        } else {
            return null;
        }
    }

    /**
     * Retrieve the title.
     * @return The title string.
     */
    public final String getTitle() {
        return getPropertyString("title");
    }

    /**
     * Retrieve the message body.
     * @return The body string.
     */
    public final String getBody() {
        return getPropertyString("body");
    }

    /**
     * Retrieve the user.
     * @return The user object.
     */
    public final User getSender() {
        return sender;
    }

}
